
package com.bei_en.teacher.management.service.system;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.management.model.system.bean.SystemResource;
import com.bei_en.teacher.management.model.system.condition.SystemResourceCondition;
import com.bei_en.teacher.management.model.system.vo.SystemResourceRespVo;
import com.bei_en.teacher.management.model.system.vo.SystemResourceVo;
import com.bei_en.teacher.management.persistence.dao.system.SystemResourceDao;
import com.bei_en.teacher.management.service.BaseService;

/**
 * 后台权限相关资源
 * @author Longqs
 * @date 2016-6-15 15:09:26
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SystemResourceService extends BaseService {

    @Autowired
    private SystemResourceDao systemResourceDao;

    public void insert(SystemResourceVo model) {

	systemResourceDao.insert(model.getSystemResource());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(SystemResourceVo model) {
//
//	systemResourceDao.insert(model.getSystemResource());
//	return systemResourceDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

	systemResourceDao.delete(modelInteger);
    }

    public SystemResourceVo findById(Integer modelInteger) {

	SystemResourceVo model = new SystemResourceVo(systemResourceDao.findById(modelInteger));
	return model;
    }

    public void update(SystemResourceVo model) {

	systemResourceDao.update(model.getSystemResource());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(SystemResourceVo model) {

	systemResourceDao.updateSelective(model.getSystemResource());
    }

    public int countAll() {

	return systemResourceDao.countAll();
    }

    public List<SystemResourceVo> findAll() {

	List<SystemResourceVo> list = new ArrayList<SystemResourceVo>();
	List<SystemResource> list1 = systemResourceDao.findAll();
	for (SystemResource model : list1) {
	    list.add(new SystemResourceVo(model));
	}
	return list;
    }

    public List<SystemResourceRespVo> findAllResp() {

	List<SystemResourceRespVo> list = new ArrayList<SystemResourceRespVo>();
	List<SystemResource> list1 = systemResourceDao.findAll();
	for (SystemResource model : list1) {
	    list.add(new SystemResourceRespVo(model));
	}
	return list;
    }

    public List<Integer> findAllIds() {

	return systemResourceDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<SystemResourceVo> findSystemResourcePage(SystemResourceCondition vo) {

	Integer totalCount = searchSystemResourceNum(vo);
	List<SystemResourceVo> list = new ArrayList<SystemResourceVo>();
	vo.setTotalCount(totalCount);

	if (totalCount>0) {
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemResource> list1 = systemResourceDao.findPageBreakByCondition(vo, rb);
	    for (SystemResource model : list1) {
		list.add(new SystemResourceVo(model));
	    }

	}

	return list;
    }

    private Integer searchSystemResourceNum(SystemResourceCondition Vo) {

	return systemResourceDao.findNumberByCondition(Vo);
    }

    /**
     * @author Guocg
     * @param userId
     * @return
     */
    public List<SystemResourceVo> findResourcesByUserId(Integer userId) {//

	List<SystemResourceVo> list = new ArrayList<SystemResourceVo>();
	List<SystemResource> list1 = systemResourceDao.findResourcesByUserId(userId);
	for (SystemResource model : list1) {
	    list.add(new SystemResourceVo(model));
	}
	return list;
    }

}
